﻿using ALMDDD.Application.Interfaces;
using ALMDDD.Application.ViewModels.SysUser;
using ALMDDD.Domain.Commands.SysUser;
using ALMDDD.Domain.Core.Bus;
using ALMDDD.Domain.Interfaces.Repository;
using ALMDDD.Domain.Model;
using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;

namespace ALMDDD.Application.Services
{
    public class SysUserService : ISysUserService
    {
        private readonly ISysUserRepository _sysUserRepository;
        private readonly IMeDiatorHandler _bus;
        private readonly IMapper _mapper;
        public SysUserService(
            ISysUserRepository sysUserRepository,
            IMapper mapper,
            IMeDiatorHandler bus
            )
        {
            _bus = bus;
            _mapper = mapper;
            _sysUserRepository = sysUserRepository;
        }
        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="sysUserViewModel"></param>
        public void Add(SysUserViewModel sysUserViewModel)
        {
            var addCommand = _mapper.Map<AddUserCommand>(sysUserViewModel);
            _bus.SendCommand(addCommand);
        }

        public void Dispose()
        {
            GC.SuppressFinalize(this);
        }
        /// <summary>
        /// 获取所有信息
        /// </summary>
        /// <returns></returns>
        public IEnumerable<SysUserViewModel> GetAll()
        {
            return _mapper.Map<IEnumerable<SysUserViewModel>>(_sysUserRepository.GetAll());
        }

        public SysUserViewModel GetById(Guid id)
        {
            return _mapper.Map<SysUserViewModel>(_sysUserRepository.GetById(id));
        }

        public void Remove(Guid id)
        {
            _sysUserRepository.Remove(id);
        }

        public void Update(SysUserViewModel sysUserViewModel)
        {
            _sysUserRepository.Update(_mapper.Map<Sys_User>(sysUserViewModel));
        }
        public SysUserViewModel GetMany(Expression<Func<Sys_User, bool>> where)
        {
            return _mapper.Map<SysUserViewModel>(_sysUserRepository.GetMany(where));
        }

        public SysUserListsVO GetUserRoles(Guid id)
        {
            return _mapper.Map<SysUserListsVO>(_sysUserRepository.GetUserRoles(id));
        }
    }
}
